package com.blacktech.dbu.auth.entity;

import com.blacktech.dbu.core.common.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 操作日志实体
 *
 * @author Yanyunsong
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "dbu_operation_log")
public class DbuOperationLog extends BaseEntity {

    /**
     * 操作用户
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false, foreignKey = @ForeignKey(name = "fk_operation_log_user"))
    @JsonIgnore
    private DbuUser user;

    /**
     * 操作类型
     */
    @NotBlank(message = "操作类型不能为空")
    @Size(max = 50, message = "操作类型不能超过50个字符")
    @Column(name = "operation_type", nullable = false, length = 50)
    private String operationType;

    /**
     * 操作描述
     */
    @Size(max = 1000, message = "操作描述不能超过1000个字符")
    @Column(name = "operation_desc", length = 1000)
    private String operationDesc;

    /**
     * 操作对象类型
     */
    @Size(max = 50, message = "操作对象类型不能超过50个字符")
    @Column(name = "object_type", length = 50)
    private String objectType;

    /**
     * 操作对象ID
     */
    @Column(name = "object_id")
    private Long objectId;

    /**
     * IP地址
     */
    @Size(max = 45, message = "IP地址不能超过45个字符")
    @Column(name = "ip_address", length = 45)
    private String ipAddress;

    /**
     * 用户代理信息
     */
    @Size(max = 500, message = "用户代理信息不能超过500个字符")
    @Column(name = "user_agent", length = 500)
    private String userAgent;

    /**
     * 操作结果
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "operation_result", length = 20)
    private OperationResult operationResult;

    /**
     * 异常信息
     */
    @Size(max = 1000, message = "异常信息不能超过1000个字符")
    @Column(name = "error_message", length = 1000)
    private String errorMessage;

    /**
     * 操作结果枚举
     */
    public enum OperationResult {
        SUCCESS("成功"),
        FAILURE("失败"),
        ERROR("错误");

        private final String description;

        OperationResult(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}